const app = getApp();
const {upTime} = require('/util/public');
Page({
  data: {
    config: app.config,
    list:[],
    promoteId: '',
    promoteUserId: '',
    status: -1,
    page: 0,
    rows: 20,
    loadingSta: 1,
    timer: '---- / -- / --',
    timerSta: false,
    showOrderType: false,
    typeName: '所有订单',

    totalNumberCustomers: 0,
    newTotalNumberCustomers: 0,

  },
  onLoad() {
    this.getPromoteId();
  },


  datePicker() {
    let timer = new Date();
    let todayTimer = upTime("%Y-%M-%D",timer);
    
    my.datePicker({
      currentDate: todayTimer,
      startDate: '2000-1-1',
      endDate: '2100-1-1',
      success: (res) => {
        
        let arr = res.date.split('-');
        let TimeType = arr[0] + '/' + arr[1] + '/' + arr[2];
        this.setData({
          timer: TimeType,
          timerSta: true,
          page: 0,
          list: [],
        })
        this.getList();
      },
    });
  },


  // popup
  selectType(){
    this.setData({showOrderType: true});
  },
  onPopupClose(){
      this.setData({showOrderType: false});
  },

  setType(e){
    console.log(555,e);
    let state = e.target.dataset.state;
    if(state.sta == -500){
      this.onPopupClose();
    }else{
      this.setData({
        status: state.sta,
        page: 0,
        list: [],
        typeName: state.name
      })
      this.onPopupClose();
      this.getList();
    }
  },


  // 加载
  lower(){
    console.log(1111);
    this.getList();
  },


  // 获取推广员id
  getPromoteId(){
    let user = my.getStorageSync({ key: 'user' });
    let data = {
      list:[],
      promoteId: '',
      promoteUserId: '',
      status: -1,
      page: 0,
      rows: 20,
      loadingSta: 1,
      timer: '---- / -- / --',
      timerSta: false,
      showOrderType: false,
      typeName: '所有订单',

      totalNumberCustomers: 0,
      newTotalNumberCustomers: 0,
    }
    this.setData(data);

    if (user.data) {
        console.log("user", user)
        this.setData({
          promoteId: user.data.promoteId,
          promoteUserId: user.data.promoteUserId,
        })
        this.getList();
    } else {
        this.setData({
          promoteId: '',
          promoteUserId: '',
        })
    }
  },


  // 获取订单列表
  getList(){
    if(!this.data.promoteUserId){
      my.showToast({
        content:'未获取到推广员信息'
      })
      return;
    }

    let pages = this.data.page;
    pages += 1;

    let dat = {
      promoteUserId: this.data.promoteUserId,
      // promoteUserId: 72,
      status: this.data.status,
      page: pages,
      rows: this.data.rows
    }

    
    if(this.data.timerSta){
      // let timeArr = this.data.timer.split('/');
      // let newTimerType = timeArr[0] + '-' + timeArr[1] + '-' + timeArr[2];
      dat.startTime = this.data.timer + ' 00:00:00';
      dat.endTime = this.data.timer + ' 23:59:59';
    }

    my.showLoading({
      content: '加载中...',
    });
    this.setData({
      loadingSta: 2,
    })

    app.user.pageByPromoteUserId(dat)
    .then(res=>{
      console.log(777,res);
      
      if(res.data && res.data.list.length > 0){

        this.setData({
          totalNumberCustomers: res.data.list[0].totalNumberCustomers,
          newTotalNumberCustomers: res.data.list[0].newTotalNumberCustomers,
        })
        
        let newList = this.data.list;
        this.setListType(res.data.list)
          .then(res => {
            newList.push(...res);

            this.setData({
              list: newList,
              page: pages,
              loadingSta: 1,
            })
          })

      }else{

        this.setData({
          loadingSta: 3,
        })

      }
      my.hideLoading();
    })
    .catch(err=>{
      my.hideLoading();
    })

  },




  setListType(list){
    return new Promise((relose, reject)=>{
      list.forEach((item) => {
        if(item.userVo){
          let timer = item.userVo.createTimeStr.split(' ')[0];
          let timeArr = timer.split('-');
          item.userVo.createTimeStrFront = timeArr[0] + '.' + timeArr[1] + '.' + timeArr[2];

          let phoneStr = JSON.stringify(item.userVo.phone);
          item.userVo.phoneEx = phoneStr.substring(0,3) + '******' + phoneStr.substring(phoneStr.length-2,phoneStr.length) ;
        }
          
      })
      relose(list)
    })
  },




  // 跳转
  navigateTo(e){
    app.navigateTo(e)
  },



});
